package com.ygqh.baby.controller.kids.model;

import com.ygqh.baby.ao.*;
import com.ygqh.baby.kids.entity.bo.KidsOrder;
import com.ygqh.baby.kids.entity.bo.KidsOrderDetail;
import com.ygqh.baby.po.YgOrder;
import com.ygqh.baby.po.YgOrderDetail;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class YgOrderBuilder {

    private KidsOrder kidsOrder;

    private List<KidsOrderDetail> kidsOrderDetailList;

    private YgOrderBuilder() {
    }

    public YgOrderBuilder(KidsOrder kidsOrder, List<KidsOrderDetail> kidsOrderDetailList) {
        this.kidsOrder = kidsOrder;
        this.kidsOrderDetailList = kidsOrderDetailList;
    }

    public YgOrder builder() {
        return doBuilder();
    }

    private YgOrder doBuilder() {
        Assert.notNull(kidsOrder, "kidsOrder con't be null");
        Assert.notEmpty(kidsOrderDetailList, "kidsOrderDetailList con't be null/empty");
        YgOrder order = doBuilderOrder();
        List<YgOrderDetail> orderDetailList = doBuilderOrderDetail();
        order.setOrderDetail(orderDetailList);
        return order;
    }

    private YgOrder doBuilderOrder() {
        YgOrder order = new YgOrder();
        order.setUserId(kidsOrder.getUserId());
        order.setOrderId(Long.parseLong(kidsOrder.getOrderCode()));
        order.setParentId(Long.parseLong(kidsOrder.getParentCode()));
        order.setAppType(AppType.Small);
        order.setOrderStatus(OrderStatus.WaitingSend);
        order.setOrderType(getYgOrderType());
        order.setSupplierId(kidsOrder.getSupplierId());

        order.setReceiver(kidsOrder.getReceiver());
        order.setProvinceName(kidsOrder.getProvinceName());
        order.setCityName(kidsOrder.getCityName());
        order.setAreaName(kidsOrder.getAreaName());
        order.setAddress(kidsOrder.getAddress());
        order.setTelPhone(kidsOrder.getTelPhone());

        order.setProductPrice(kidsOrder.getProductPrice());
        order.setManjianPrice(kidsOrder.getMjDisPrice());
        order.setBmdisPrice(kidsOrder.getBmDisPrice());
        order.setAccountPrice(kidsOrder.getIntegral());
        order.setYouhuijuanPrice(kidsOrder.getYhqDisPrice());
        order.setTotalPrice(kidsOrder.getTotalPrice());
        order.setExpressPrice(kidsOrder.getExpressPrice());

        order.setVipDisPrice(BigDecimal.ZERO);
        order.setGroupDisPrice(BigDecimal.ZERO);
        order.setPackDisPrice(BigDecimal.ZERO);
        order.setNewCustomDisPrice(BigDecimal.ZERO);

        order.setPayType(PayType.Small);
        order.setPaidPrice(kidsOrder.getPaidPrice());
        order.setPaidTime(kidsOrder.getPayTime());

        order.setCreateTime(kidsOrder.getCreateTime());
        order.setStatus(DataStatus.Valid);
        order.setUserRemark(kidsOrder.getUserRemark());
        order.setSourceCode(kidsOrder.getSourceCode());
        order.setCommentStatus(CommentStatus.UnComplete);
        order.setPlatformNo(kidsOrder.getPlatformNo());
        order.setPlatformName(kidsOrder.getPlatformName());
        return order;

    }

    private OrderType getYgOrderType() {
        if ("GroupBuy".equals(kidsOrder.getOrderType())) {
            return OrderType.GroupBuying;
        }
        return OrderType.Ordinary;
    }

    private List<YgOrderDetail> doBuilderOrderDetail() {
        List<YgOrderDetail> orderDetailList = new ArrayList<>(kidsOrderDetailList.size());
        for (KidsOrderDetail orderDetail : kidsOrderDetailList) {
            YgOrderDetail detail = new YgOrderDetail();
            detail.setOrderId(Long.parseLong(kidsOrder.getOrderCode()));
            detail.setSupplierId(orderDetail.getSupplierId());
            detail.setProductName(orderDetail.getProductName());
            detail.setSkuCode(orderDetail.getSkuCode());
            detail.setSkuId(orderDetail.getSkuId());
            detail.setShortName(orderDetail.getColor());
            detail.setSize(orderDetail.getSizeName());
            detail.setImageUrl(orderDetail.getImageUrl());
            detail.setSalePrice(orderDetail.getSalePrice());
            detail.setUnitName(orderDetail.getUnitName());
            detail.setQuantity(orderDetail.getQuantity());
            detail.setPromotionType(getDetailPromotionType(orderDetail.getPromotionType()));
            detail.setDetailStatus(OrderDetailStatus.WaitingSend);
            detail.setProductId(orderDetail.getProductId());
            detail.setRefundStatus(RefundStatus.NoRefund);
            detail.setRemark(orderDetail.getId().toString());
            detail.setPackDisPrice(orderDetail.getPackDisPrice());
            detail.setRealPrice(orderDetail.getTotalPrice());
            detail.setBmDisPrice(orderDetail.getBmDisPrice());
            detail.setMjDisPrice(orderDetail.getMjDisPrice());
            detail.setAccountPrice(orderDetail.getIntegral());
            detail.setNewCustomDisPrice(BigDecimal.ZERO);
            detail.setForeverRate(BigDecimal.ZERO);
            detail.setShareRate(BigDecimal.ZERO);
            detail.setProductCode(orderDetail.getProductCode());
            orderDetailList.add(detail);
        }
        return orderDetailList;

    }

    private PromotionType getDetailPromotionType(String type) {
        if ("GroupBuy".equals(type)) {
            return PromotionType.GroupBuying;
        }
        return PromotionType.Ordinary;
    }

}
